cmake_minimum_required(VERSION 3.20.0)
project(onvif_tool VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
#include(thirdlibex)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWITH_NONAMESPACES  -DWITH_DOM -DWITH_OPENSSL -Wall -g")

set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)

include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/include/gsl ${CMAKE_SOURCE_DIR}/include/gsoapHead ${CMAKE_SOURCE_DIR}/include/hyperHead)

find_package(OpenSSL REQUIRED)

set(ONVIF_TOOL TRUE)

if(ONVIF_TOOL)
    set(sources ${CMAKE_SOURCE_DIR}/src/gsoapSrc/dom.cpp
                ${CMAKE_SOURCE_DIR}/src/IOnvifTool.cpp
                ${CMAKE_SOURCE_DIR}/src/IOnvifToolClass.cpp
                ${CMAKE_SOURCE_DIR}/src/jsonOperation.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/mecevp.cpp
                ${CMAKE_SOURCE_DIR}/src/onvifToolClass.cpp
                ${CMAKE_SOURCE_DIR}/src/utils.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/smdevp.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/soapC.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/soapClient.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/stdsoap2.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/struct_timeval.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/threads.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/wsaapi.cpp
                ${CMAKE_SOURCE_DIR}/src/gsoapSrc/wsseapi.cpp)

    add_library(${PROJECT_NAME} SHARED ${sources})
    target_link_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_CRYPTO_LIBRARY})
    target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
    if (CMAKE_SYSTEM_NAME MATCHES "Linux")
        target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto)
    elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
        target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL OpenSSL::Crypto)
    endif()

    if(CMAKE_COMPILER_IS_GNUCXX)
        # 解决windows下节数超过对象文件格式限制的错误
        # 检查GNU编译是否支持 -Wa,-mbig-obj 
        include(CheckCXXCompilerFlag)
        check_cxx_compiler_flag("-Wa,-mbig-obj" GNU_BIG_OBJ_FLAG_ENABLE)
    endif()
    target_compile_options(${PROJECT_NAME} 
            PRIVATE 
            $<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
            $<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${GNU_BIG_OBJ_FLAG_ENABLE}>>:-Wa,-mbig-obj>) 

    # 设置生成的库的版本信息
    set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})

    # 安装库和头文件
    install(TARGETS ${PROJECT_NAME}
        EXPORT ${PROJECT_NAME}Targets
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
    )
    install(FILES ${CMAKE_SOURCE_DIR}/include/IOnvifTool.h DESTINATION include)
    
    target_include_directories(${PROJECT_NAME} INTERFACE
    $<INSTALL_INTERFACE:include>  # 安装时替换为实际的安装目录
    )

    target_link_directories(${PROJECT_NAME} INTERFACE
    $<INSTALL_INTERFACE:lib>  # 安装时替换为实际的安装目录
    )

    # 生成并安装 CMake 包的配置文件
    include(CMakePackageConfigHelpers)
    write_basic_package_version_file(
    ${PROJECT_NAME}ConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
    )

    configure_package_config_file(
    ${CMAKE_SOURCE_DIR}/cmake_in/onvifToolConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
    INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}
    )

    # 安装配置文件
    install(FILES
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
    DESTINATION lib/cmake/${PROJECT_NAME}
    )

    # 安装导出的目标文件
    install(EXPORT ${PROJECT_NAME}Targets
    FILE ${PROJECT_NAME}Targets.cmake
    NAMESPACE ${PROJECT_NAME}::
    DESTINATION lib/cmake/${PROJECT_NAME}
    # 包含头文件路径
    # INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
 )

endif()

#单元测试
# if(CMAKE_BUILD_TYPE STREQUAL "Debug")
#     enable_testing()
#     set(test_sources ${CMAKE_SOURCE_DIR}/src/gsoapSrc/dom.cpp
#                     ${CMAKE_SOURCE_DIR}/src/main.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/mecevp.cpp
#                     ${CMAKE_SOURCE_DIR}/src/onvifToolClass.cpp
#                     ${CMAKE_SOURCE_DIR}/src/jsonOperation.cpp
#                     ${CMAKE_SOURCE_DIR}/src/jsonOperationTest.cpp
#                     ${CMAKE_SOURCE_DIR}/src/onvifToolClassTest.cpp
#                     ${CMAKE_SOURCE_DIR}/src/utils.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/smdevp.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/soapC.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/soapClient.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/stdsoap2.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/struct_timeval.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/threads.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/wsaapi.cpp
#                     ${CMAKE_SOURCE_DIR}/src/gsoapSrc/wsseapi.cpp)
#     add_executable(onvifToolClass_test ${test_sources})

#     target_include_directories(onvifToolClass_test PRIVATE ${OPENSSL_INCLUDE_DIR})
#     target_link_directories(onvifToolClass_test PRIVATE ${OPENSSL_CRYPTO_LIBRARY})
#     if (CMAKE_SYSTEM_NAME MATCHES "Linux")
#         target_link_libraries(onvifToolClass_test  PRIVATE OpenSSL::SSL OpenSSL::Crypto GTest::gtest GTest::gmock)
#     elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
#         target_link_libraries(onvifToolClass_test  PRIVATE OpenSSL::SSL OpenSSL::Crypto GTest::gtest GTest::gmock)
#     endif()
    
#     if(CMAKE_COMPILER_IS_GNUCXX)
#         # 解决windows下节数超过对象文件格式限制的错误
#         # 检查GNU编译是否支持 -Wa,-mbig-obj 
#         include(CheckCXXCompilerFlag)
#         check_cxx_compiler_flag("-Wa,-mbig-obj" GNU_BIG_OBJ_FLAG_ENABLE)
#     endif()
#     target_compile_options(onvifToolClass_test 
#             PRIVATE 
#             $<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
#             $<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${GNU_BIG_OBJ_FLAG_ENABLE}>>:-Wa,-mbig-obj>) 
#     add_test(NAME onvifToolClass_test
#              COMMAND onvifToolClass_test)
# endif()

add_subdirectory(${CMAKE_SOURCE_DIR}/src/example)